#include "editwindow.h"
#include "ui_editwindow.h"
#include "editevent.h"
#include <QMessageBox>

EditWindow::EditWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditWindow)
{
    ui->setupUi(this);
}

EditWindow::~EditWindow()
{
    delete ui;
}

EditWindow::EditWindow(QDate chsnDate, EventsHandler *events) : ui(new Ui::EditWindow), chsDate(chsnDate), evnts(events), ptmp(NULL)
{
    ui->setupUi(this);
    ui->date_label->setText(chsnDate.toString("yyyy.MM.dd"));
    FillTree();
}

void EditWindow::on_EditButton_clicked()
{
    ptmp = evnts->GetEvent(c_tmp);
    if (ptmp)
    {
        EditEvent *edit = new EditEvent(ptmp);
        edit->show();
        this->close();
    }
    else
        QMessageBox::warning(0,"Error!", "Select something first!");
}

void EditWindow::on_RemoveButton_clicked()
{
    ptmp = evnts->GetEvent(c_tmp);
    if (ptmp)
    {
        evnts->RemoveEvent(ptmp);
        FillTree();
    }
    else
        QMessageBox::warning(0,"Error!", "Select something first!");
}

void EditWindow::on_treeWidget_itemClicked(QTreeWidgetItem* item, int column)
{
    c_tmp.dateTime.setTime(QTime::fromString(item->text(0),"hh:mm:ss"));
    c_tmp.description = item->text(1);
    c_tmp.dateTime.setDate(chsDate);
}

void EditWindow::FillTree()
{
    ui->treeWidget->clear();
    EventList *eventsList,*tmp;
    eventsList = NULL;
    tmp = NULL;
    eventsList = evnts->FindEventsByDate(chsDate);
    if (eventsList)
    {
        QTreeWidgetItem *A;
        A = new QTreeWidgetItem(0);
        A->setText(0, eventsList->Data.dateTime.time().toString());
        A->setText(1, eventsList->Data.description);
        ui->treeWidget->addTopLevelItem(A);
        tmp = eventsList->next;
        while (tmp)
        {
            QTreeWidgetItem *A;
            A = new QTreeWidgetItem(0);
            A->setText(0, tmp->Data.dateTime.time().toString());
            A->setText(1, tmp->Data.description);
            ui->treeWidget->addTopLevelItem(A);
            tmp = tmp->next;
        }
    }
}
